package com.funo.wcity.app.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * 
 * @author ljb
 * 
 */
public class URLReader {

	private static Log log = LogFactory.getLog(URLReader.class);
	public static String COMM_HEADER = "COMM_HEADER";
	public static String COMM_FOOTER = "COMM_FOOTER";
	/**
	 * URL
	 * 
	 * @param urlpath
	 * @param code
	 * @return
	 */
	public static String readUrl(String urlpath, String code) {
		HttpURLConnection con = null;
		URL url = null;
		try {
			url = new URL(urlpath);
			con = (HttpURLConnection) url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					con.getInputStream(), code));
			StringBuilder b = new StringBuilder();
			String line;
			while ((line = reader.readLine()) != null) {
				b.append(line);
				b.append("\r\n");
			}
			String s = b.toString();
			if(StringUtil.isEmpty(s))
				log.info("返回url结果为空");
			return s;
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}

	}
	/**
	 * 添加头尾栏目
	 * @param request 请求
	 * @param pingStr 地址
	 */
 	public static void setCommHeadAndFoot(HttpSession session, String pingStr)
			throws Exception {
 		session.setAttribute(
				COMM_HEADER,
				URLReader.readUrl(
						"http://appheaderandfootserver.ompfj.com.cn/header.do?"+pingStr,"utf-8"));
 		session.setAttribute(
				COMM_FOOTER,
				URLReader.readUrl(
						"http://appheaderandfootserver.ompfj.com.cn/footer.do?"+pingStr,"utf-8"));
	}
}
